package lee.vioson.remotedata

import android.content.Context
import lee.vioson.remotedata.utils.CacheUtils
import lee.vioson.repository.BuildConfig
import java.lang.ref.WeakReference

object EvnConstants {
    private const val CURRENT_EVN = "CURRENT_EVN"
    const val dev_evn = "dev_evn"
    const val test_evn = "test_evn"
    const val release_evn = "release_evn"

    private var contextWf = WeakReference<Context>(null)

    var currentEvn = dev_evn
        set(value) {
            field = value
            contextWf.get()?.let {
                CacheUtils.getInstance(it).put(CURRENT_EVN, value)
            }
        }
        get() = contextWf.get()?.let { CacheUtils.getInstance(it).getString(CURRENT_EVN) }
            ?: dev_evn

    val evn: Evn
        get() = when (currentEvn) {
            dev_evn -> DevEvn()
            test_evn -> TestEvn()
            release_evn -> ReleaseEvn()
            else -> TestEvn()
        }

    fun initEvn(context: Context, evn: String) {
        contextWf = WeakReference(context)
        if (BuildConfig.DEBUG) {
            if (currentEvn != evn) {
                currentEvn = evn
            }
        } else {
            currentEvn = release_evn
        }
    }
}

interface Evn {
    val baseUrl: String
}

class DevEvn : Evn {
    override val baseUrl: String
        get() = "http://49.234.45.112:8080/"
}

class TestEvn : Evn {
    override val baseUrl: String
        get() = "http://49.234.45.112:8080/"
}

class ReleaseEvn : Evn {
    override val baseUrl: String
        get() = "http://49.234.45.112:8080/"
}
